package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Db.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import flash.events.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.rules.*;
    import mortal.game.scene.map.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.autoFight.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class AutoFightBriefController extends Controller
    {
        private var _autoFightInfo:Object;
        private var _isSet:Boolean = false;
        private var _isSetDefault:Boolean = false;
        private var _isBeginFight:Boolean = false;
        private var _autoFightComplex:AutoFightComplexModule;
        private var _autoFightBrief:AutoFightBriefModule;

        public function AutoFightBriefController()
        {
            
        }

        public function get autoFightBrief() : AutoFightBriefModule
        {
            if (this._autoFightBrief == null)
            {
                this._autoFightBrief = new AutoFightBriefModule();
                this._autoFightBrief.addEventListener(Event.ADDED_TO_STAGE, this.onBriefAddToStageHandler);
            }
            return this._autoFightBrief;
        }

        public function get autoFightComplex() : AutoFightComplexModule
        {
            if (this._autoFightComplex == null)
            {
                this._autoFightComplex = new AutoFightComplexModule();
                this._autoFightComplex.addEventListener(Event.ADDED_TO_STAGE, this.onComplexAutoAddToStageHandler);
            }
            return this._autoFightComplex;
        }

        private function onBriefAddToStageHandler(event:Event) : void
        {
            this.autoFightBrief.update(AutoFight.localAutoFight);
            if (cache.guide.autoFightGuide == true)
            {
                this.autoFightBrief.guideAutoFight();
            }
            
        }

        private function onComplexAutoAddToStageHandler(event:Event) : void
        {
            this.autoFightComplex.update(AutoFight.localAutoFight);
            
        }

        override protected function initView() : IView
        {
            return this.autoFightBrief;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.SaveAutoFightInfo, this.onSaveAutoFightInfoHandler);
            Dispatcher.addEventListener(EventName.SaveDefaultAutoFight, this.onSaveDefaultInfoHandler);
            Dispatcher.addEventListener(EventName.SaveAndBeginToFight, this.onSaveAndBeginToFightHandler);
            Dispatcher.addEventListener(EventName.AutoFightEnd, this.onAutoFightEndHandler);
            Dispatcher.addEventListener(EventName.AutoFightKeyToFight, this.OnAutoFightKeyToFightHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateAutoFightSetting, this.onReceiveAutoFightInfoHandler);
            NetDispatcher.addCmdListener(ServerCommand.SkillUpdate, this.onSaveWhenSkillUpdateHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdataHandler);
            Dispatcher.addEventListener(EventName.AutoFightComplexOpen, this.onAutoFightComplexOpenHandler);
            Dispatcher.addEventListener(EventName.AutoFightBriefOpen, this.onAutoFightBriefOpenHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskAdd, this.onTaskAddHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskDel, this.onTaskDelHandler);
            Dispatcher.addEventListener(EventName.Guide_Hang, this.onGuideHangHandler);
            Dispatcher.addEventListener(EventName.SaveAutoFightInfoForOther, this.onSaveInfoForOtherHandler);
            
        }

        private function onSaveAutoFightInfoHandler(event:DataEvent) : void
        {
            this.saveAndUpdateServer(true, false);
            
        }

        private function onSaveDefaultInfoHandler(event:DataEvent) : void
        {
            this.saveAndUpdateServer(true, false);
            this.autoFightComplex.update(AutoFight.localAutoFight);
            
        }

        private function onSaveAndBeginToFightHandler(event:DataEvent) : void
        {
            this.saveAndUpdateServer(true, true);
            
        }

        private function onSaveInfoForOtherHandler(event:DataEvent) : void
        {
            this.saveAndUpdateServer(false, false);
            if (!this.autoFightBrief.isHide)
            {
                this.autoFightBrief.update(AutoFight.localAutoFight);
            }
            else if (!this.autoFightComplex.isHide)
            {
                this.autoFightComplex.update(AutoFight.localAutoFight);
            }
            
        }

        private function saveAndUpdateServer(param1:Boolean = true, param2:Boolean = false) : void
        {
            var _loc_3:* = AutoFight.localAutoFight.serializeString();
            GameProxy.autoFightProxy.saveAutoFightInfo(_loc_3);
            AutoFight.currentAutoFight.copy(AutoFight.localAutoFight);
            if (param1 == false)
            {
                
            }
            if (param2)
            {
                if (RolePlayer.instance.swimming)
                {
                    AutoFight.currentAutoFight.isAutoFight = false;
                    AutoFight.localAutoFight.isAutoFight = false;
                    MsgManager.addBroadCast("游泳状态下不可战斗");
                }
                else
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.BeginToFight));
                }
            }
            else
            {
                MsgManager.addBroadCast("自动战斗设置成功");
                Dispatcher.dispatchEvent(new DataEvent(EventName.SetAutoFightSuccess));
            }
            
        }

        private function onSaveWhenSkillUpdateHandler(param1:Object) : void
        {
            var _loc_9:TSkill = null;
            var _loc_10:int = 0;
            var _loc_11:int = 0;
            var _loc_12:int = 0;
            var _loc_13:int = 0;
            var _loc_14:Array = null;
            var _loc_15:int = 0;
            var _loc_16:int = 0;
            var _loc_17:int = 0;
            var _loc_18:int = 0;
            var _loc_2:* = cache.skill.skills;
            var _loc_3:Array = [];
            var _loc_4:int = 0;
            while (_loc_4 < _loc_2.length)
            {
                
                if (_loc_2[_loc_4].useType == ESkillUseType._ESkillUseTypeInitiative)
                {
                    _loc_3.push(_loc_2[_loc_4]);
                }
                _loc_4++;
            }
            var _loc_5:* = param1 as SkillInfo;
            var _loc_6:* = (param1 as SkillInfo).position;
            var _loc_7:* = AutoFight.localAutoFight.skillArr;
            var _loc_8:* = _loc_3.length;
            if (_loc_3.length > 0 && _loc_8 <= 3)
            {
                _loc_10 = 0;
                while (_loc_10 < _loc_8)
                {
                    
                    _loc_9 = _loc_3[_loc_10] as TSkill;
                    if (_loc_9.skillLevel == 1 && _loc_5.tSkill.skillLevel == 1)
                    {
                        _loc_7[_loc_10] = _loc_9.skillId;
                    }
                    else if (_loc_7[_loc_10] != 0)
                    {
                        _loc_11 = SkillRules.getPostionById(_loc_7[_loc_10]);
                        if (_loc_11 == _loc_6)
                        {
                            _loc_7[_loc_10] = _loc_5.tSkill.skillId;
                        }
                    }
                    _loc_10++;
                }
            }
            else
            {
                _loc_12 = _loc_7.length;
                _loc_13 = 0;
                while (_loc_13 < _loc_12)
                {
                    
                    if (_loc_7[_loc_13] != 0)
                    {
                        _loc_17 = SkillRules.getPostionById(_loc_7[_loc_13]);
                        if (_loc_17 == _loc_6)
                        {
                            _loc_7[_loc_13] = _loc_5.tSkill.skillId;
                            break;
                        }
                    }
                    _loc_13++;
                }
                _loc_14 = AutoFight.localAutoFight.skillForOtherArr;
                _loc_15 = _loc_14.length;
                _loc_16 = 0;
                while (_loc_16 < _loc_15)
                {
                    
                    if (_loc_14[_loc_16] != 0)
                    {
                        _loc_18 = SkillRules.getPostionById(_loc_14[_loc_16]);
                        if (_loc_18 == _loc_6)
                        {
                            _loc_14[_loc_16] = _loc_5.tSkill.skillId;
                            break;
                        }
                    }
                    _loc_16++;
                }
            }
            this.saveAndUpdateServer(false, false);
            this.autoFightBrief.update(AutoFight.localAutoFight);
            
        }

        private function onReceiveAutoFightInfoHandler(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SAutoFightSetting;
            var _loc_3:* = _loc_2.jsStr;
            if (_loc_3 != "")
            {
                AutoFight.currentAutoFight.deserializeObj(_loc_3);
                AutoFight.localAutoFight.copy(AutoFight.currentAutoFight);
            }
            else if (!this._isSetDefault)
            {
                this._isSetDefault = true;
                this.saveAndUpdateServer(false, false);
            }
            
        }

        private function onSceneUpdataHandler(event:DataEvent) : void
        {
            this.findMonsters();
            if (this.autoFightComplex.isHide == false)
            {
                this.autoFightComplex.attackPanel.update(true);
            }
            else
            {
                this.autoFightComplex.isSceneUpdate = true;
            }
            
        }

        private function findMonsters() : void
        {
            var _loc_2:Array = null;
            var _loc_3:TBoss = null;
            var _loc_4:Array = null;
            AutoFight.localAutoFight.monsterArr.length = 0;
            var _loc_1:* = AutoFight.localAutoFight.monsterArr;
            if (this._autoFightComplex != null && !this._autoFightComplex.attackPanel.isTask)
            {
                _loc_2 = cache.scene.getFightRefreshPoint();
            }
            else
            {
                _loc_2 = cache.scene.getFightRefreshPoint();
                _loc_4 = cache.task.getTaskMonster();
                _loc_2 = MonstersUtil.getTaskMonsters(_loc_2, _loc_4);
            }
            for each (_loc_3 in _loc_2)
            {
                
                _loc_1[_loc_1.length] = _loc_3.code;
            }
            AutoFight.currentAutoFight.monsterArr = _loc_1.concat();
            trace("findMonsters:" + AutoFight.localAutoFight.monsterArr);
            
        }

        private function onAutoFightEndHandler(event:DataEvent) : void
        {
            this._isBeginFight = false;
            AutoFight.currentAutoFight.isAutoFight = false;
            this.autoFightBrief.onAutoFightEndHandler();
            this.autoFightComplex.autoFightEnd();
            Dispatcher.dispatchEvent(new DataEvent(EventName.OverFight));
            
        }

        private function OnAutoFightKeyToFightHandler(event:DataEvent) : void
        {
            if (!AutoFight.currentAutoFight.isAutoFight)
            {
                if (RolePlayer.instance.swimming)
                {
                    MsgManager.addBroadCast("游泳状态下不可战斗");
                }
                else
                {
                    AutoFight.currentAutoFight.isAutoFight = true;
                    AutoFight.localAutoFight.isAutoFight = true;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.BeginToFightShortcut));
                }
            }
            else
            {
                AutoFight.currentAutoFight.isAutoFight = false;
                AutoFight.localAutoFight.isAutoFight = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.OverFight));
            }
            
        }

        private function onAutoFightComplexOpenHandler(event:DataEvent) : void
        {
            this.autoFightComplex.show();
            this.autoFightBrief.hide();
            
        }

        private function onAutoFightBriefOpenHandler(event:DataEvent) : void
        {
            this.autoFightBrief.show();
            this.autoFightComplex.hide();
            
        }

        private function onTaskAddHandler(param1:Object) : void
        {
            var _loc_3:Array = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:TaskTargetInfo = null;
            var _loc_7:Array = null;
            var _loc_8:Boolean = false;
            if (param1 == null)
            {
                
            }
            var _loc_2:* = param1 as TaskInfo;
            if (_loc_2 != null)
            {
                _loc_3 = _loc_2.getMonsterList();
                if (_loc_3 == null || _loc_3.length == 0)
                {
                    
                }
                MonstersUtil.taskAddMonArr.length = 0;
                _loc_4 = 0;
                while (_loc_4 < _loc_3.length)
                {
                    
                    _loc_6 = _loc_3[_loc_4];
                    if (_loc_6.mapId == MapFileUtil.mapID)
                    {
                        MonstersUtil.taskAddMonArr.push(BossConfig.instance.getInfoByName(_loc_6.id));
                    }
                    _loc_4++;
                }
                _loc_7 = AutoFight.localAutoFight.monsterArr;
                _loc_8 = false;
                _loc_4 = 0;
                while (_loc_4 < _loc_3.length)
                {
                    
                    for each (_loc_5 in _loc_7)
                    {
                        
                        if (_loc_5 == _loc_3[_loc_4].id)
                        {
                            _loc_3.splice(_loc_4, 1);
                            _loc_8 = true;
                            _loc_4 = _loc_4 - 1;
                            break;
                        }
                    }
                    if (_loc_8)
                    {
                        break;
                    }
                    _loc_4++;
                }
                for each (_loc_6 in _loc_3)
                {
                    
                    _loc_7.push(_loc_6.id);
                }
                AutoFight.localAutoFight.monsterArr = _loc_7;
                this.saveAndUpdateServer(false, false);
                if (this._autoFightComplex != null && this._autoFightComplex.isHide == false)
                {
                    this.autoFightComplex.attackPanel.update(true);
                }
            }
            
        }

        private function onTaskDelHandler(param1:Object) : void
        {
            var _loc_3:TaskTargetInfo = null;
            MonstersUtil.taskAddMonArr.length = 0;
            AutoFight.localAutoFight.monsterArr.length = 0;
            var _loc_2:* = cache.task.getTaskMonster();
            for each (_loc_3 in _loc_2)
            {
                
                AutoFight.localAutoFight.monsterArr.push(_loc_3.id);
            }
            
        }

        private function onTaskUpdateHandler(param1:Array) : void
        {
            var _loc_3:TaskInfo = null;
            var _loc_5:Array = null;
            var _loc_6:Array = null;
            var _loc_7:TaskTargetInfo = null;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            if (param1 == null)
            {
                
            }
            var _loc_2:Boolean = false;
            var _loc_4:int = 0;
            while (_loc_4 < param1.length)
            {
                
                if (param1[_loc_4] != null)
                {
                    _loc_3 = param1[_loc_4] as TaskInfo;
                    if (_loc_3.status == ETaskStatus._ETaskStatusHadCompleted)
                    {
                        _loc_2 = true;
                        break;
                    }
                }
                else
                {
                    break;
                }
                _loc_4++;
            }
            if (_loc_2)
            {
                _loc_5 = cache.task.getTaskMonster();
                _loc_6 = AutoFight.localAutoFight.monsterArr;
                _loc_4 = 0;
                while (_loc_4 < _loc_5.length)
                {
                    
                    _loc_9 = 0;
                    while (_loc_9 < _loc_6.length)
                    {
                        
                        _loc_7 = _loc_5[_loc_4];
                        if (_loc_6[_loc_9] == _loc_7.id)
                        {
                            _loc_5.splice(_loc_4, 1);
                            _loc_6.splice(_loc_9, 1);
                            break;
                        }
                        _loc_9++;
                    }
                    _loc_4++;
                }
                for each (_loc_7 in _loc_5)
                {
                    
                    _loc_6.push(_loc_7.id);
                }
                AutoFight.localAutoFight.monsterArr = _loc_6;
            }
            
        }

        private function onGuideHangHandler(event:DataEvent) : void
        {
            if (this.autoFightBrief.isHide)
            {
                this.autoFightBrief.show();
            }
            else
            {
                this.autoFightBrief.guideAutoFight();
            }
            
        }

    }
}
